package solutions.leetcode.normal

import solutions.lib.ListNode

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/merge-in-between-linked-lists/">1669. 合并两个链表</a>
 */
private class Solution1669 {
    fun mergeInBetween(list1: ListNode?, a: Int, b: Int, list2: ListNode?): ListNode? {
        val root = ListNode()
        root.next = list1
        var p : ListNode? = root
        var end = list2
        while (end?.next != null) {
            end = end.next
        }

        var ca = a
        var cb = b
        while (p != null) {
            if (ca == 0) {
                val t = ListNode()
                t.next = p.next
                p.next = list2
                p = t
                ca--
                continue
            }
            if (cb == 0) {
                end?.next = p.next?.next
                break
            }
            p = p.next
            ca--
            cb--
        }
        return root.next
    }
}

fun main() {
    
}